package com.chenyue.cm.moudules.${vo.moudule}.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.R;
import com.chenyue.cm.common.constant.Constants;
import com.chenyue.cm.common.utils.PasswordHelper;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.moudules.${vo.moudule}.service.${vo.tablename_uperCase}Service;
import com.chenyue.cm.${vo.moudule}.domain.${vo.tablename_uperCase};
import com.chenyue.cm.${vo.moudule}.domain.${vo.tablename_uperCase}Example;

/**
 * 
 *
 * @Description: ${vo.describe}
 * @file:
 * @throws
 * @date ${vo.dateTime}
 */
@Controller
@RequestMapping("/${vo.tablename_camelCase}")
public class ${vo.tablename_uperCase}Controller {


    @Autowired
    private ${vo.tablename_uperCase}Service ${vo.tablename_camelCase}Service;



    private String page_url_list = C.THEME_Prefix + "${vo.moudule}/${vo.tablename_lowerCase}/${vo.tablename_camelCase}List";
    private String page_url_form = C.THEME_Prefix + "${vo.moudule}/${vo.tablename_lowerCase}/${vo.tablename_camelCase}Form";


    @RequestMapping("/toList")
    public String toList(HttpServletRequest request, HttpServletResponse response, Model model) {
        return page_url_list;
    }


    @RequestMapping("/toForm")
    public String toForm(Model model, HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ${vo.tablename_uperCase} vo = (id != null && id != "") ? ${vo.tablename_camelCase}Service.selectByPrimaryKey(id) : new ${vo.tablename_uperCase}();
        model.addAttribute("vo", vo);
        return page_url_form;
    }


    @RequestMapping("/getList")
    @ResponseBody
    public PageVo<${vo.tablename_uperCase}> getList(HttpServletRequest request, HttpServletResponse response) {
        Pageable pageInfo = RequestUtil.getPageable(request);
		PageVo<${vo.tablename_uperCase}>  result  = ${vo.tablename_camelCase}Service.getPageBean(pageInfo,${vo.tablename_uperCase}.class);
		return result;
    }


    /**
     * @param vo
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(${vo.tablename_uperCase} vo, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");

        if (id != null && id != "") {
            ${vo.tablename_camelCase}Service.getBaseMapper().updateById(vo);
        } else {
            vo.setId(UUID.randomUUID().toString().replace("-", ""));
            ${vo.tablename_camelCase}Service.insert(vo);
        }

        return R.success();
    }


    /**
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable String id, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");

        ${vo.tablename_uperCase}Example aExample = new ${vo.tablename_uperCase}Example();
        aExample.createCriteria().andIdEqualTo(id);
        ${vo.tablename_camelCase}Service.deleteByExample(aExample);

        return R.success();
    }
 	
 	/**
	 * 【API】----  删除多个数据
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public R delete(HttpServletRequest request, HttpServletResponse response, Model model) {
		response.setContentType("text/html;charset=utf-8");
		String ids =  request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
        	${vo.tablename_camelCase}Service.deleteByPrimaryKey(idString);
		}
		return R.success();
	}
 	    
}
